<template>
  <div class="container">
    <el-row>
      <el-card shadow="never">
        <div slot="header">
          <el-form :inline="true" :model="searchForm" size="small">
            <el-form-item label="名称" class="el-search-item">
              <el-input v-model="searchForm.parameterName" placeholder="模糊查询" clearable></el-input>
            </el-form-item>
            <el-form-item label="编码" class="el-search-item">
              <el-input v-model="searchForm.numb" placeholder="精准查询" clearable></el-input>
            </el-form-item>
			<el-form-item label="类型" prop="dataType">
				<el-select v-model="searchForm.dataType" placeholder="选择" clearable>
					<el-option :value="1" label="单选框"></el-option>
					<el-option :value="2" label="复选框"></el-option>
					<el-option :value="3" label="下拉框"></el-option>
					<el-option :value="4" label="输入框"></el-option>
					<el-option :value="5" label="文本框"></el-option>
					<el-option :value="6" label="富文本输入域"></el-option>
				</el-select>
			</el-form-item>
            <el-form-item class="el-search-item">
              <el-button type="success" plain icon="el-icon-search" @click="getDataList">查询</el-button>
            </el-form-item>
            <el-form-item class="el-search-item">
              <el-button v-if='hasPerm("sys:sysParameter:add")' type="primary" plain icon="el-icon-plus" @click="openAdd">新增</el-button>
            </el-form-item>
          </el-form>
        </div>
        <!--数据表格 element ui-->
        <el-table :data="tableData" :height="tableHeight" style="width: 100%">
          <el-table-column prop="parameterName" label="名称" align="center" sortable></el-table-column>
		  <el-table-column prop="numb" label="编码" align="center" sortable></el-table-column>
          <el-table-column label="类型" align="center" sortable>
			<template slot-scope="scope">
				<span v-if="scope.row.dataType==1">单选框</span><span v-if="scope.row.dataType==2">复选框</span><span v-if="scope.row.dataType==3">下拉框</span>
				<span v-if="scope.row.dataType==4">输入框</span><span v-if="scope.row.dataType==5">文本框</span><span v-if="scope.row.dataType==6">富文本输入域</span>
			</template>
		  </el-table-column>
          <el-table-column prop="dataValue" label="内容" align="center" sortable show-overflow-tooltip></el-table-column>
          <el-table-column prop="seq" label="排序" align="center" sortable></el-table-column>
		  <el-table-column prop="msg" label="备注" align="center" sortable show-overflow-tooltip></el-table-column>
          <el-table-column v-if='hasPerm("sys:sysParameter:list")' label="更多操作" align="center" width="250">
            <template slot-scope="scope">
              <el-button v-if='hasPerm("sys:sysParameter:edit")' @click="openUpdate(scope.row)" size="mini" type="text" icon="el-icon-edit">编辑</el-button>
              <el-button v-if='hasPerm("sys:sysParameter:del")' @click="deleteFun(scope.row)" size="mini" type="text" icon="el-icon-delete">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页组件-->
        <el-pagination style="padding:0;text-align: center;" background @size-change="handleSizeChange" @current-change="handleCurrentChange" layout="total, sizes, prev, pager, next, jumper"
                       :current-page="pageNum" :page-sizes="[8, 15, 20]" :page-size="pageSize" :total="total">
        </el-pagination>
      </el-card>
      <!--新增用户对话框-->
      <el-dialog :title="dialog.dialogTitle" :visible.sync="dialog.visible" width="35%">
        <el-form :rules="rules" ref="sysParameterForm" :model="sysParameterForm" size="small" label-width="80px">
          <el-form-item prop="parameterName" label="名称">
            <el-input v-model="sysParameterForm.parameterName" placeholder="请输入名称" auto-complete="off"></el-input>
          </el-form-item>
		  <el-form-item prop="dataType" label="类型">
		  	<el-select v-model="sysParameterForm.dataType" placeholder="选择" clearable>
		  		<el-option :value="1" label="单选框"></el-option>
		  		<el-option :value="2" label="复选框"></el-option>
		  		<el-option :value="3" label="下拉框"></el-option>
		  		<el-option :value="4" label="输入框"></el-option>
		  		<el-option :value="5" label="文本框"></el-option>
		  		<el-option :value="6" label="富文本输入域"></el-option>
		  	</el-select>
		  </el-form-item>
		  <el-form-item prop="numb" label="编码">
		    <el-input v-model="sysParameterForm.numb" placeholder="请输入编码" auto-complete="off"></el-input>
		  </el-form-item>
          <el-form-item prop="dataValue" :label="sysParameterForm.dataValueLabel">
            <el-input v-model="sysParameterForm.dataValue" :placeholder="sysParameterForm.dataValueLabel" auto-complete="off"></el-input>
          </el-form-item>
		  <el-form-item prop="seq" label="排序">
		    <el-input v-model="sysParameterForm.seq" placeholder="整数,降序" auto-complete="off"></el-input>
		  </el-form-item>
          <el-form-item prop="msg" label="备注">
            <el-input v-model="sysParameterForm.msg" placeholder="请输入备注" auto-complete="off"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialog.visible = false" size="small">取 消</el-button>
          <el-button type="primary" @click="submitFun()" :loading="loading.status" size="small">{{loading.status ? loading.SubText : loading.BtnText}}</el-button>
        </span>
      </el-dialog>
    </el-row>
  </div>
</template>
<script>
  import sys_sysParameter from "@/api/sys/sysParameter";
  export default {
    data() {
      return {
        // 表格数据
        tableData: [],
        // 分页数据
        total: 0,
        pageNum: 1,
        pageSize: 8,
        // 表格高度
        tableHeight: 0,
        // 用来存放日期组件所选中的值
        checkedTime: '',
        // 查询参数
        searchForm: {parameterName: null, numb: null, dataType: null},
        // 弹出层
        dialog: {flag: 0, dialogTitle: "", visible: false},
        // 菜单弹出层
        menuDialog: {dialogTitle: "", visible: false},
        // 表单参数
        sysParameterForm: {
          sysParameterId: null,
          sysCompanyId: null,
          parameterName: null,
          dataValue: null,
          dataType: 3,
          msg: null,
          numb: null,
          seq: 1,
		  dataValueLabel: '键值对',
        },
        // 提交按钮文字
        loading: {status: false,BtnText: '确 定',SubText: '提交中...'},
        // 表单校验
        rules: {
          parameterName: [{required: true,trigger: "change",message: "请输入名称"}],
          dataValue: [{required: true,trigger: "change",message: "请输入内容"}],
          dataType: [{required: true,trigger: "change",message: "请输入类型"}],
          numb: [{required: true,trigger: "change",message: "请输入编码"}],
          seq: [{required: true,trigger: "change",message: "请输入排序"}],
        },
      };
    },
    mounted() {
      this.$nextTick(() => {
        this.tableHeight = window.innerHeight - 205;
      });
    },
    created() {
      this.getDataList();
    },
    watch: {
      "sysParameterForm.dataType"(value) {
        if (value != null && value>3) {
          this.sysParameterForm.dataValueLabel="默认值";
        }else{
			this.sysParameterForm.dataValueLabel="键值对";
		}
      }
    },
    methods: {
      handleSizeChange(val) {
        this.pageSize = val
        this.getDataList()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.getDataList()
      },
      // 获取列表
      getDataList() {
        sys_sysParameter.list({
          current: this.pageNum,
          size: this.pageSize,
          sysParameterId: this.searchForm.sysParameterId,
          startTime: this.searchForm.startTime,
          endTime: this.searchForm.endTime
        }).then(res => {
          if(res.data.code === 2000) {
            this.tableData = res.data.data.records;
            this.total = res.data.data.total;
          }
        });
      },
      // 新增按钮操作
      openAdd() {
        this.dialog.flag = 0;
        this.resetForm("sysParameterForm");
        this.dialog.dialogTitle = "新增";
        this.dialog.visible = true;
      },
      // 更新按钮操作
      openUpdate(row) {
        this.dialog.flag = 1;
        this.resetForm("sysParameterForm");
        this.dialog.dialogTitle = "编辑";
        this.dialog.visible = true;
        this.getById(row.sysParameterId);
      },
      // 提交数据操作
      submitFun() {
        this.$refs.sysParameterForm.validate(valid => {
          if(valid){
            this.loading.status = true;
            sys_sysParameter.saveOrUpdate(this.sysParameterForm,this.dialog.flag).then(res => {
              if (res.data.code === 2000) {
                this.$message.success(res.data.msg);
                if(this.dialog.flag === 0){
                  this.pageNum = 1;
                }
                this.getDataList();
                // 关闭弹框
                this.dialog.visible = false;
                // 关闭加载
                this.loading.status = false;
              }else{
                this.$message.error(res.data.msg);
                this.loading.status = false;
                return false;
              }
            })
          }
        })
      },
      // 根据id获取信息
      getById(id) {
        sys_sysParameter.getById(id).then(res => {
          if (res.data.code === 2000) {
            this.sysParameterForm.sysParameterId = res.data.data.sysParameterId;
            this.sysParameterForm.sysCompanyId = res.data.data.sysCompanyId;
            this.sysParameterForm.parameterName = res.data.data.parameterName;
            this.sysParameterForm.dataValue = res.data.data.dataValue;
            this.sysParameterForm.dataType = res.data.data.dataType;
            this.sysParameterForm.msg = res.data.data.msg;
            this.sysParameterForm.numb = res.data.data.numb;
            this.sysParameterForm.seq = res.data.data.seq;
            this.sysParameterForm.deleted = res.data.data.deleted;
          }
        });
      },
      // 根据id删除
      deleteFun(row) {
        this.$confirm("确定删除当前吗？", "系统提示", {confirmButtonText: "确定", cancelButtonText: "取消", type: 'error'
        }).then(() =>{
            sys_sysParameter.remove(row.sysParameterId).then(res => {
            if(res.data.code === 2000){
              this.$message.success(res.data.msg);
              this.getDataList()
            }else {
              this.$message.error(res.data.msg);
            }
          })
        }).catch(() => {
          this.$message.info("取消删除");
        });
      },
      // 清空表单
      resetForm(formName) {
        if (this.$refs[formName]) {
          this.$refs[formName].resetFields();
        }
      },
    }
  }
</script>
<style lang="less" scoped>
  .el-search-item {
    margin-bottom: 0;
  }
  .el-card/deep/.el-card__body {
    padding: 0 10px 10px 10px !important;
  }
  .el-card/deep/.el-card__header {
    border-bottom: none !important;padding: 10px 10px !important;
  }
</style>